<?php

namespace App\Library\Utils;

class Url
{
    /**
     * 判断是否绝对路径
     *
     * @param $url
     * @return bool
     */
    public static function isAbsolute($url): bool
    {
        $pattern = "/^(?:ftp|https?|feed)?:?\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?]
        (?:[\w#!:\.\?\+\|=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi";

        return (bool) preg_match($pattern, $url);
    }

    public static function getAbsolute($url): string
    {
        if (static::isAbsolute($url)) {
            return $url;
        }

        $baseUrl = kg_cos_url();
        return $baseUrl.$url;
    }
}